//
//  WelComeViewController.swift
//  sinaweibo
//
//  Created by mac on 15/11/12.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class WelComeViewController: UIViewController {

    
    override func loadView() {
//        super.loadView()
        view = backView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    private func startAnimation() {
    
        iconImage.snp_updateConstraints { (make) -> Void in
            
            make.bottom.equalTo(view.snp_bottom).offset(-self.view.bounds.size.height + 180)
        }
        
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            //将提前改变的约束 进行更新
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.6, animations: { () -> Void in
                    self.userLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        print("OK")
                        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewControler, object: nil)
                })
        }
    }
    
    private func setupUI() {
        
        view.addSubview(iconImage)
        view.addSubview(userLabel)
        
        iconImage.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        
        userLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(iconImage.snp_bottom).offset(20)
            
        }
        userLabel.alpha = 0
        
        iconImage.sd_setImageWithURL(UserAccountViewModel().headURL, placeholderImage: UIImage(named:"avatar_default_big"))
        iconImage.layer.cornerRadius = 45
        iconImage.layer.masksToBounds = true
    }
    
    private lazy var backView =  UIImageView(image: UIImage(named:"ad_background"))
    private lazy var iconImage = UIImageView()
    
    private lazy var userLabel : UILabel = UILabel(title: "\((UserAccountViewModel().userName ?? "") + " 欢迎回来")", size: 19, color: UIColor.lightGrayColor())

}
